package pl.textures.game
{
	import pl.error.InternalError;
	import pl.textures.Textures;
	import pl.textures.packs.TexturePacks;

	import starling.textures.Texture;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * ...
	 * @author Bakudo
	 */
	public class SingleTexture extends TextureSize implements ISingleTexture
	{
		private static var secureFunction:Function;
		private var _texture:Texture;
		private var _bitmapData:BitmapData;
		private var _uid:uint;
		private var _pivotX:uint;
		private var _pivotY:uint;

		public function SingleTexture( size:Point, _texture:Texture, _bitmap:Bitmap, _uid:uint, _pivotX:uint, _pivotY:uint )
		{
			super( size );
			this._texture = _texture;
			this._bitmapData = _bitmap.bitmapData.clone();
			this._uid = _uid;
			this._pivotX = _pivotX;
			this._pivotY = _pivotY;
			if (secureFunction == null) throw new Error( InternalError.CREATION_ERROR );
		}

		override protected function toOverride():void
		{
		}

		public static function factory( uid:uint, textureId:uint, texturePack:uint = 0, packRegion:Rectangle = null, _pivot:Point = null ):ISingleTexture
		{
			secureFunction = function():void
			{
			};
			var tData:Array = createTexture( textureId, texturePack, packRegion );
			return new SingleTexture( new Point( tData[0].width, tData[0].height ), tData[0], tData[1], uid, _pivot == null ? 0 : _pivot.x, _pivot == null ? 0 : _pivot.y );
		}

		/**
		 * zwraca texture za pomoca ID lub texture z texture pack'a
		 * @param	textureId
		 * @param	texturePack
		 * @param	packRegion
		 * @return
		 */
		public static function createTexture( textureId:uint, texturePack:uint = 0, packRegion:Rectangle = null ):Array
		{
			if (texturePack == TexturePacks.NO_TEXTURE_PACK)
				return Textures.getTextureAndBitmapById( textureId );
			else
				return Textures.getTextureAndBitmapFromRegion( texturePack, packRegion );

			return null;
		}

		public function get texture():Texture
		{
			return _texture;
		}

		public function get bitmapData():BitmapData
		{
			return _bitmapData;
		}

		public function get uid():uint
		{
			return _uid;
		}

		public function get pivotX():uint
		{
			return _pivotX;
		}

		public function get pivotY():uint
		{
			return _pivotY;
		}
	}
}